﻿/*
* edirauth - http://edirauth.codeplex.com
* A .NET library and utility for authenticating to eDirectory
* Copyright (c) 2013 Matt Shirilla (http://shirilla.com)
* Licensed under the Microsoft Public License (MS-RL)
* http://edirauth.codeplex.com/license
*/

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using edirAuth;

namespace edirAuthTests
{
    [TestClass]
    public class AuthenticateTests
    {

        EdirAuthorizer auth;

        [TestInitialize()]
        public void Initialize() {
            auth = Helper.EdirAuthorizerFactory();
        }

        /// <summary>
        /// User should successfully authenticate known good credentials of an account with no restrictive attributes present (ex: loginDisabled)
        /// </summary>
        [TestMethod]
        public void validCredentials() {
            AuthInfo authInfo = auth.authenticate(Helper.TESTUSER01_UID, Helper.TESTUSERS_PASSWORD);
            Assert.AreEqual(ResultCodes.OK, authInfo.PasswordResult.Code, "Password:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_PASSWORD_OKAY, authInfo.PasswordResult.Desc, "Password:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LoginDisabledResult.Code, "loginDisabled:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_LOGINDISABLED_NOTPRESENT, authInfo.LoginDisabledResult.Desc, "loginDisabled:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LockedByIntruderResult.Code, "lockedByIntruder:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_INTRUDERLOCKED_NOTPRESENT, authInfo.LockedByIntruderResult.Desc, "lockedByIntruder:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.ExpiredResult.Code, "loginExpirationTime:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_EXPIRED_FALSE, authInfo.ExpiredResult.Desc, "loginExpirationTime:The wrong description was returned");
            Assert.IsTrue(authInfo.allowLogin, "allowLogon:The wrong value was returned");
        }

        /// <summary>
        /// User should fail to authenticate due to a bad password an account with no restrictive attributes present (ex: loginDisabled)
        /// </summary>
        [TestMethod]
        public void badPassword() {
            AuthInfo authInfo = auth.authenticate(Helper.TESTUSER01_UID, "this is a very bad password indeed!");
            Assert.AreEqual(ResultCodes.BAD_PASS, authInfo.PasswordResult.Code, "password:The wrong code was returned");
            Assert.AreEqual(ResultCodes.BAD_PASS_DESC, authInfo.PasswordResult.Desc, "password:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LoginDisabledResult.Code, "loginDisabled:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_LOGINDISABLED_NOTPRESENT, authInfo.LoginDisabledResult.Desc, "loginDisabled:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LockedByIntruderResult.Code, "lockedByIntruder:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_INTRUDERLOCKED_NOTPRESENT, authInfo.LockedByIntruderResult.Desc, "lockedByIntruder:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.ExpiredResult.Code, "loginExpirationTime:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_EXPIRED_FALSE, authInfo.ExpiredResult.Desc, "loginExpirationTime:The wrong description was returned");
            Assert.IsFalse(authInfo.allowLogin, "allowLogon:The wrong value was returned");
        }

        /// <summary>
        /// User should fail to authenticate due to loginDisabled=false
        /// </summary>
        [TestMethod]
        public void loginDisabled() {
            AuthInfo authInfo = auth.authenticate(Helper.TESTUSER02_UID, Helper.TESTUSERS_PASSWORD);
            Assert.AreEqual(ResultCodes.OK, authInfo.PasswordResult.Code, "pass:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_PASSWORD_OKAY, authInfo.PasswordResult.Desc, "pass:The wrong description was returned");
            Assert.AreEqual(ResultCodes.LOGINDISABLED_TRUE, authInfo.LoginDisabledResult.Code, "loginDisabled:The wrong code was returned");
            Assert.AreEqual(ResultCodes.LOGINDISABLED_TRUE_DESC, authInfo.LoginDisabledResult.Desc, "loginDisabled:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LockedByIntruderResult.Code, "lockedByIntruderThe wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_INTRUDERLOCKED_NOTPRESENT, authInfo.LockedByIntruderResult.Desc, "lockedByIntruder:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.ExpiredResult.Code, "loginExpirationTime:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_EXPIRED_NOTPRESENT, authInfo.ExpiredResult.Desc, "loginExpirationTime:The wrong description was returned");
            Assert.IsFalse(authInfo.allowLogin, "allowLogon:The wrong value was returned");
        }

        /// <summary>
        /// User should fail to authenticate due to lockedByIntruder=true
        /// </summary>
        [TestMethod]
        public void lockedByIntruder() {
            AuthInfo authInfo = auth.authenticate(Helper.TESTUSER03_UID, Helper.TESTUSERS_PASSWORD);
            Assert.AreEqual(ResultCodes.OK, authInfo.PasswordResult.Code, "pass:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_PASSWORD_OKAY, authInfo.PasswordResult.Desc, "pass:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LoginDisabledResult.Code, "loginDisabled:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_LOGINDISABLED_NOTPRESENT, authInfo.LoginDisabledResult.Desc, "loginDisabled:The wrong description was returned");
            Assert.AreEqual(ResultCodes.INTRUDERLOCKOUT_TRUE, authInfo.LockedByIntruderResult.Code, "lockedByIntruderThe wrong code was returned");
            Assert.AreEqual(ResultCodes.INTRUDERLOCKOUT_TRUE_DESC, authInfo.LockedByIntruderResult.Desc, "lockedByIntruder:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.ExpiredResult.Code, "loginExpirationTime:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_EXPIRED_NOTPRESENT, authInfo.ExpiredResult.Desc, "loginExpirationTime:The wrong description was returned");
            Assert.IsFalse(authInfo.allowLogin, "allowLogon:The wrong value was returned");
        }

        /// <summary>
        /// User should fail to authenticate due to loginExpirationTime set to past
        /// </summary>
        [TestMethod]
        public void loginExpired() {
            AuthInfo authInfo = auth.authenticate(Helper.TESTUSER04_UID, Helper.TESTUSERS_PASSWORD);
            Assert.AreEqual(ResultCodes.OK, authInfo.PasswordResult.Code, "pass:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_PASSWORD_OKAY, authInfo.PasswordResult.Desc, "pass:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LoginDisabledResult.Code, "loginDisabled:The wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_LOGINDISABLED_NOTPRESENT, authInfo.LoginDisabledResult.Desc, "loginDisabled:The wrong description was returned");
            Assert.AreEqual(ResultCodes.OK, authInfo.LockedByIntruderResult.Code, "lockedByIntruderThe wrong code was returned");
            Assert.AreEqual(ResultCodes.OK_INTRUDERLOCKED_NOTPRESENT, authInfo.LockedByIntruderResult.Desc, "lockedByIntruder:The wrong description was returned");
            Assert.AreEqual(ResultCodes.EXPIRED, authInfo.ExpiredResult.Code, "loginExpirationTime:The wrong code was returned");
            Assert.AreEqual(ResultCodes.EXPIRED_DESC, authInfo.ExpiredResult.Desc, "loginExpirationTime:The wrong description was returned");
            Assert.IsFalse(authInfo.allowLogin, "allowLogon:The wrong value was returned");
        }



    }//class
}//namespace
